package org.zanpu.timer.task;

import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zanpu.timer.HttpClient.HTTPAsync;
import org.zanpu.timer.HttpClient.vo.HttpVO;
import org.zanpu.timer.Utils.XmlUtils;
import org.zanpu.timer.pojo.XmlTagPojo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;


/**
 * 执行任务配置
 * Created by MD on 2016/12/27.
 */
@Service
public class DoTask {

    private String path;

    @Autowired
    private HTTPAsync httpAsync;

    public DoTask() {
        path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    }

    public void doTimer() {
        try {
            new XmlUtils(
                    new FileInputStream(path.replace("%20", " ") + "timerServer-config.xml")
            ).timerServerXML().parallelStream().forEach(this::sendApi
            );
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 执行请求
     *
     * @param map 配置参数
     */
    private void sendApi(Map<String, Object> map) {
        try {
            Calendar nakeder = Calendar.getInstance();
            String time = nakeder.get(Calendar.HOUR_OF_DAY) + ":" + nakeder.get(Calendar.MINUTE);
            HttpVO httpVO = new HttpVO();
            long timestamp = new Date().getTime() / 1000;
            switch (map.get(XmlTagPojo.TYPE.getName()).toString()) {
                case "alarm":
                    if (time.equals(map.get(XmlTagPojo.VALUE.getName()).toString())) {
                        if (MainTask.lastTime.get(map.get(XmlTagPojo.KEY.getName())) != null) {
                            if ((timestamp - MainTask.lastTime.get(map.get(XmlTagPojo.KEY.getName()))) >= 60)
                                biuldVo(httpVO, map, timestamp);
                        } else {
                            biuldVo(httpVO, map, timestamp);
                        }
                    }
                    break;
                case "mechanically":
                    if (MainTask.lastTime.get(map.get(XmlTagPojo.KEY.getName())) != null) {
                        if ((timestamp - MainTask.lastTime.get(map.get(XmlTagPojo.KEY.getName()))) >= Integer.parseInt(map.get(XmlTagPojo.VALUE.getName()).toString())) {
                            if (Boolean.valueOf(map.get(XmlTagPojo.ASYN.getName()).toString())) {
                                if (MainTask.result.get(map.get(XmlTagPojo.KEY.getName()).toString()))
                                    biuldVo(httpVO, map, timestamp);
                            } else {
                                biuldVo(httpVO, map, timestamp);
                            }
                        }
                    } else {
                        biuldVo(httpVO, map, timestamp);
                    }
                    break;
            }
            if (httpVO.getUrl() != null && !httpVO.getUrl().isEmpty()) {
                MainTask.result.put(map.get(XmlTagPojo.KEY.getName()).toString(), false);
                String result = httpAsync.SendRequest(httpVO);
                MainTask.result.put(map.get(XmlTagPojo.KEY.getName()).toString(), result.equals("true"));
            }
        } catch (Exception e) {
            MainTask.result.put(map.get(XmlTagPojo.KEY.getName()).toString(), true);
            e.printStackTrace();
        }
    }

    /**
     * 构造Vo对象
     *
     * @param httpVO    vo对象
     * @param map       配置参数
     * @param timestamp 时间戳
     * @return HttpVO
     */
    public HttpVO biuldVo(HttpVO httpVO, Map map, long timestamp) {
        httpVO.setUrl(map.get(XmlTagPojo.URL.getName()).toString());
        httpVO.setHttpType(map.get(XmlTagPojo.TYPE.getName()).toString());
        httpVO.setMethod(map.get(XmlTagPojo.METHOD.getName()).toString());
        MainTask.lastTime.put(map.get(XmlTagPojo.KEY.getName()).toString(), timestamp);
        return httpVO;
    }
}
